//
//  IGSelectDrawVC.m
//  jingdongfang
//
//  Created by RKL on 2018/5/24.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGSelectDrawVC.h"
#import "FindLayout.h"
#import "MoreArteditCell.h"
#import "ClickImageViewController.h"

@interface IGSelectDrawVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
{
    UICollectionView *collect;

}

/**
 记录上一次点击的item
 */
@property (nonatomic , strong) NSIndexPath * lastIndexPath;
@end

@implementation IGSelectDrawVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"选择封面";
    self.view.backgroundColor = RGB(242, 244, 248);
    UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureClick:)];
    [right setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB(0, 171, 230)} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    //默认选第一张
    IGArtWorkModel * model = self.dataArr[0];
    model.isSelect = @"1";
    
    [self setCollectionview];
}

#pragma mark - 右上角确定按钮点击事件
- (void)sureClick:(UIBarButtonItem *)sender
{
    NSLog(@"右上角确定按钮点击事件");
    
    IGArtWorkModel * model = self.dataArr[self.lastIndexPath.item];
    
    ClickImageViewController * vc = [[ClickImageViewController alloc] init];
    vc.url = model.image;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setCollectionview
{
    FindLayout *layout = [[FindLayout alloc] init];
    collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavigationMagin, KSCREENWIDTH, KSCREENHEIGHT-NavigationMagin) collectionViewLayout:layout];
    collect.backgroundColor = [UIColor clearColor];
    collect.delegate = self;
    collect.dataSource = self;
    if (@available(iOS 11.0, *))
    {
        collect.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        collect.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
        collect.scrollIndicatorInsets = collect.contentInset;
    }
    collect.alwaysBounceVertical = YES;
    [collect registerClass:[MoreArteditCell class] forCellWithReuseIdentifier:@"MoreArteditCell"];
    [self.view addSubview:collect];
//    selArr=[NSMutableArray array];
    
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.dataArr.count>0?self.dataArr.count:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MoreArteditCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreArteditCell" forIndexPath:indexPath];
    IGArtWorkModel * artWorkModel = self.dataArr[indexPath.item];
    cell.data = artWorkModel;
    cell.isSel = [artWorkModel.isSelect isEqualToString:@"1"] ? YES : NO;
    [cell setNeedsLayout];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    IGArtWorkModel * artWorkModel = self.dataArr[indexPath.item];
    artWorkModel.isSelect = [artWorkModel.isSelect isEqualToString:@"1"] ? @"0" : @"1";
    
    BOOL isEqual = ([indexPath compare:self.lastIndexPath] == NSOrderedSame) ? YES : NO;
    
    if (!isEqual)
    {
        IGArtWorkModel * artWorkModel = self.dataArr[self.lastIndexPath.item];
        artWorkModel.isSelect = @"0";
    }
    
    self.lastIndexPath = indexPath;
    
    [collectionView reloadData];

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel * base =_dataArr[indexPath.item];
    if ([base.plates integerValue] == 1)
    {
        //横板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1080/1920));
    }
    else if ([base.plates integerValue]==2)
    {
        //坚板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1920/1080));
    }
    return CGSizeMake(0, 0);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


- (void)dealloc
{
    
}

@end



















